package com.example.makerspace.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.CompetitionSubmissionDO;
import com.example.makerspace.mapper.CompetitionSubmissionMapper;
import com.example.makerspace.vo.CompetitionSubmissionVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
@Component
public class CompetitionSubmissionDAO extends  BaseDAO<CompetitionSubmissionDO>{

    @Resource
    private CompetitionSubmissionMapper competitionSubmissionMapper;

    @PostConstruct
    public void init(){
        this.mapper = competitionSubmissionMapper;
    }

    public List<CompetitionSubmissionDO> selectByUsername(String username) {
        QueryWrapper<CompetitionSubmissionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return   competitionSubmissionMapper.selectList(queryWrapper);
    }

    public void deleteByUsernameAndCompetitionId(String username, Integer id) {
        QueryWrapper<CompetitionSubmissionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",id);
        competitionSubmissionMapper.delete(queryWrapper);

    }

    public List<CompetitionSubmissionDO> selectByUsernameAndId(String username, Integer competitionId) {
        QueryWrapper<CompetitionSubmissionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",competitionId);
        return  competitionSubmissionMapper.selectList(queryWrapper);
    }

    public void deleteByUsername(String username) {
        competitionSubmissionMapper.delete(new QueryWrapper<CompetitionSubmissionDO>().eq("username",username));
    }

    public void updateByUsername(CompetitionSubmissionDO competitionSubmissionDO) {
        UpdateWrapper<CompetitionSubmissionDO> wrapper = new UpdateWrapper<>();
        wrapper.eq("username",competitionSubmissionDO.getUsername());
        mapper.update(competitionSubmissionDO,wrapper);
    }

    public IPage<CompetitionSubmissionVO> selectByPage(Page<CompetitionSubmissionVO> page, String name) {
        return page.setRecords(competitionSubmissionMapper.getCompetitionSumbission(page,name));
    }

    public IPage<CompetitionSubmissionVO> selectByPageAndAll(Page<CompetitionSubmissionVO> page) {
        return page.setRecords(competitionSubmissionMapper.getCompetitionSumbissionNO(page));

    }

    public IPage<CompetitionSubmissionVO> selectByPageNameAndState(Page<CompetitionSubmissionVO> page, String name, String state) {
        return page.setRecords(competitionSubmissionMapper.selectByPageNameAndState(page,name,state));
    }
}
